package party.lovey.remote.utils;

import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.util.Date;

/**
 * Created by huhaichao on 2017/3/19.
 */
public class RobotUtil {
    public static Robot robot;
    public static int screenWidth;
    public static int screenHeight;

    static{
        try {
            long time1 = new Date().getTime();
            robot=new Robot();
            long time2 = new Date().getTime();
            System.out.println("robot time use = "+(time2-time1)/1000.0);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            screenHeight= (int) screenSize.getHeight();
            screenWidth= (int) screenSize.getWidth();
            long time3 = new Date().getTime();
            System.out.println("util init total time use = "+(time3-time1)/1000.0);
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    /**
     * 截取全屏
     * @return
     */
    public static BufferedImage getScreenImage(){
       return getScreenImage(0,0,screenWidth,screenHeight);
    }

    /**
     * 截取自定义区域
     * @param beginX
     * @param bginY
     * @param width
     * @param height
     * @return
     */
    public static BufferedImage getScreenImage(int beginX, int bginY, int width, int height){
        long time1 = new Date().getTime();
        BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(beginX, bginY, width, height));
        long time2 = new Date().getTime();
        System.out.println("get screen image time use = "+(time2-time1)/1000.0);
        return screenCapture;
    }

    public static boolean clickPoint(Point point){
        robot.mouseMove((int)point.getX(),(int)point.getY());
        robot.delay(Consts.MOUSE_DELAY);
        robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
        robot.delay(Consts.MOUSE_DELAY);
        robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
        return true;
    }
}
